package com.symc.sims0213xfw.service.impl;

import com.symc.sims0213xfw.bean.Classes;
import com.symc.sims0213xfw.dao.ClassesDao;
import com.symc.sims0213xfw.dao.impl.ClassesDaoImpl;
import com.symc.sims0213xfw.service.ClassesService;
import com.symc.sims0213xfw.service.OperationStatusResult;

import java.util.List;

/**
 * @Author: 凤文  沈阳医学院2019级医学信息工程 0213
 * @CreateTime: 2022/01/26 13:49
 * @Description:
 */
public class ClassesServiceImpl implements ClassesService {
    private ClassesDao classesDao = new ClassesDaoImpl();

    @Override
    public List<Classes> getOptionalClassesList(Integer grade) {
        return classesDao.queryListVisibleClasses(grade);
    }

    @Override
    public List<Classes> getOptionalGrade(String departmentName) {
        return classesDao.queryListClassesGradeByDepartmentName(departmentName);
    }

    @Override
    public List<Classes> getOptionalClassesName(Integer grade) {
        return classesDao.queryListClassesNameByGrade(grade);
    }

    @Override
    public Classes getClassesId(String departmentName, Integer grade, String classesName) {
        return classesDao.queryClassesId(departmentName,grade,classesName);
    }

    @Override
    public Long addClasses(String id, String name, Integer did, Integer grade) {
        if (id.length() > Classes.ID_LENGTH) {
            return OperationStatusResult.ID_LENGTH_OUT_OF_BOUNDS;
        }

        if (name.length() > Classes.NAME_LENGTH) {
            return OperationStatusResult.NAME_LENGTH_OUT_OF_BOUNDS;
        }

        if (did > Classes.DID_MAX || did < Classes.DID_MIN) {
            return OperationStatusResult.UNREASONABLE_DID;
        }

        if (grade > Classes.GRADE_IN_DATE || grade < Classes.LOW_GRADE_IN_DATE) {
            return OperationStatusResult.UNREASONABLE_DATE;
        }

        if (classesDao.queryVisibleClasses(id) != null) {//用户可见
            return OperationStatusResult.EXIST;
        } else if (classesDao.queryListVisibleClasses(grade) == null) {//库中没有
            if (classesDao.addClasses(id, name, did, grade) > 0) {
                return OperationStatusResult.SUCCESS;
            } else {
                return OperationStatusResult.DEFEAT;
            }
        } else {//库中有，用户不可见
            if (classesDao.updateClasses(id, name, did, grade) > 0) {
                return OperationStatusResult.SUCCESS;
            } else {
                return OperationStatusResult.DEFEAT;
            }
        }
    }

    @Override
    public Long deleteClasses(String id) {
        if (classesDao.queryVisibleClasses(id) == null) {
            return OperationStatusResult.NOT_EXIST;//ok
        } else {
            if (classesDao.deleteClasses(id) > 0) {
                return OperationStatusResult.SUCCESS;
            } else {
                return OperationStatusResult.DEFEAT;
            }
        }
    }

    @Override
    public Long updateClasses(String id, String name, Integer did, Integer grade) {
        if (id.length() > Classes.ID_LENGTH) {
            return OperationStatusResult.ID_LENGTH_OUT_OF_BOUNDS;
        }

        if (name.length() > Classes.NAME_LENGTH) {
            return OperationStatusResult.NAME_LENGTH_OUT_OF_BOUNDS;
        }

        if (did > Classes.DID_MAX || did < Classes.DID_MIN) {
            return OperationStatusResult.UNREASONABLE_DID;
        }

        if (grade > Classes.GRADE_IN_DATE || grade < Classes.LOW_GRADE_IN_DATE) {
            return OperationStatusResult.UNREASONABLE_DATE;
        }

        if (classesDao.queryVisibleClasses(id) == null) {//用户不可见
            return OperationStatusResult.NOT_EXIST;
        } else {
            if (classesDao.updateClasses(id, name, did, grade) > 0) {
                return OperationStatusResult.SUCCESS;
            } else {
                return OperationStatusResult.DEFEAT;
            }
        }
    }
}
